package com.suzhe.ymall.promotion.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.common.utils.IdWorker;
import com.suzhe.ymall.promotion.enums.CouponExpireMode;
import com.suzhe.ymall.promotion.enums.CouponStatusEnum;
import com.suzhe.ymall.promotion.client.UserClient;
import com.suzhe.ymall.promotion.param.CouponPageParam;
import com.suzhe.ymall.promotion.param.SendCouponParam;
import com.suzhe.ymall.promotion.pojo.Coupon;
import com.suzhe.ymall.promotion.pojo.UserCoupon;
import com.suzhe.ymall.promotion.service.ICouponService;
import com.suzhe.ymall.promotion.service.IUserCouponService;
import com.suzhe.ymall.promotion.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-08-18
 */
@RestController
@RequestMapping("/promotion/coupon")
@Slf4j
public class CouponController {

    @Autowired
    ICouponService iCouponService;

    @Autowired
    UserClient userClient;

    @Autowired
    IdWorker idWorker;

    @Autowired
    IUserCouponService iUserCouponService;


    @GetMapping("/page")
    @LoginAuth("promotionCouponView")
    @ApiOperation(value="分页查询优惠券")
    public ApiResEntity page(CouponPageParam couponPageParam){
        log.info("couponPageParam:{}",couponPageParam);
        QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<Coupon> page = iCouponService.page(new Page<>(couponPageParam.getPageNo(),//
                couponPageParam.getPageSize()), queryWrapper);
        return ApiResEntity.ok(page);

    }

    @PostMapping("/save")
    @LoginAuth("promotionCouponSave")
    @ApiOperation(value="保存优惠券")
    public ApiResEntity save(@RequestBody Coupon coupon){
        log.info("save coupon:{}",coupon);
        coupon.setId(idWorker.nextId());
        iCouponService.save(coupon);
        return ApiResEntity.ok();

    }

    @PutMapping("/update")
    @LoginAuth("promotionCouponUpdate")
    @ApiOperation(value="更新优惠券")
    public ApiResEntity update(@RequestBody Coupon coupon){
        log.info("update coupon:{}",coupon);
        Coupon eCoupon = iCouponService.getById(coupon.getId());
        if (eCoupon == null){
            ApiResEntity.error("该优惠券不存在");
        }
        iCouponService.updateById(coupon);
        return ApiResEntity.ok();
    }

    @PostMapping("/sendCoupon")
    @LoginAuth("promotionCouponSend")
    @ApiOperation(value="发送优惠券")
    public ApiResEntity sendCoupon(@RequestBody SendCouponParam sendCouponParam){
        Coupon coupon = iCouponService.getById(sendCouponParam.getCouponId());
        List<String> phoneNumbers = sendCouponParam.getPhoneNumbers();
        List<UserCoupon> userCoupons = new ArrayList<>();
        List<UserVo> userVos = userClient.queryUserVosByPhoneNumbers(phoneNumbers);
        for(UserVo userVo: userVos){
            UserCoupon userCoupon = new UserCoupon();
            userCoupon.setId(idWorker.nextId());
            userCoupon.setUserId(userVo.getId());
            userCoupon.setCouponId(coupon.getId());
            userCoupon.setStatus(CouponStatusEnum.AVAILABLE.value());
            userCoupon.setStartTime(new Date());
            String expireMode = coupon.getExpireMode();
            Date endTime = null;
            if (CouponExpireMode.DAY.value().equals(expireMode)){
                Integer day = coupon.getExpireDay();
                endTime = DateUtils.addDays(new Date(), day);
            }else {
                endTime = coupon.getExpireTime();
            }
            userCoupon.setEndTime(endTime);
            userCoupons.add(userCoupon);
        }

        iUserCouponService.saveBatch(userCoupons);
        return ApiResEntity.ok();
    }

}
